// pages/login/login.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        account: "",
        password: "",
    },

    /**
     * 生命周期函数--页面准备就绪
     */
    onReady: function() {

        // 验证登录
        app.verifyLogin();

    },

    /**
     * 自定义函数--更改输入框
     */
    changeInput: function(e) {
        let data = {};
        data[e.target.dataset.name] = e.detail.value;
        this.setData(data);
    },

    /**
     * 自定义函数--点击登陆 
     */
    submitBind: function() {

        let data = this.data;

        if (0 === data.account.length) {
            wx.showToast({
                title: "请输入账号",
                icon: "none"
            });
            return;
        };
        if (6 > data.account.length) {
            wx.showToast({
                title: "账号长度不能少于6位",
                icon: "none"
            });
            return;
        };
        if (false === /^[a-zA-Z0-9_]{6,20}$/.test(data.account)) {
            wx.showToast({
                title: "账号只能输入字母及数字",
                icon: "none"
            });
            return;
        };
        if (0 === data.password.length) {
            wx.showToast({
                title: '请输入密码',
                icon: 'none'
            });
            return;
        };
        if (6 > data.password.length) {
            wx.showToast({
                title: "密码长度不能少于6位",
                icon: "none"
            });
            return;
        };
        if (false === /^[a-zA-Z0-9_]{6,20}$/.test(data.password)) {
            wx.showToast({
                title: "账号只能输入字母及数字",
                icon: "none"
            });
            return;
        };

        wx.showToast({
            title: '登陆中...',
            icon: 'loading',
            mask: true,
            duration: 50000
        });
        wx.request({
            url: app.data.service_url + 'shop/bind',
            header: {
                'Client-Token': app.data.client_token
            },
            data: data,
            method: 'POST',
            success: function(response) {
                wx.hideToast();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        wx.showToast({
                            title: response.data.message,
                        });
                        setTimeout(function() {
                            wx.reLaunch({
                                url: '/pages/base_mine/index/index',
                            });
                        }, 1500);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideToast();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--扫码绑定
     */
    scanningToBind: function() {
        wx.showLoading({
            title: '扫码中...',
        });
        wx.scanCode({
            onlyFromCamera: true,
            scanType: ['qrCode'],
            success: function (e) {
                wx.hideLoading();
                if ('scanCode:ok' === e.errMsg) {
                    wx.showLoading({
                        title: '准备跳转...',
                    });
                    wx.navigateTo({
                        url: '/pages/staff_mission/scanning/index?id=' + e.result,
                    });
                } else {
                    wx.showToast({
                        title: '内容不符合',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.hideLoading();
            }
        });
    }
});